<?php
/**
* 24tao前台商品详情页
* author liwei
* date 2013-07-21
*/
class GoodsAction extends Action {

	public function index(){
		import("ORG.Util.Page");
		$goodsId = $_GET['goods_id'];
		$Goods = M('goods_base_info');
		$goodsInfo = $Goods->where("goods_code=$goodsId")->find();

		//潮流明星
		$Star = M('star');
		//是否有推荐明星显示
		if(false){

		}else{//按明星随机选取
			$i=1;
			$clStar = $Star->limit('0,8')->select();
		}

		//查询评论
		$Comment = M('comment_info');
		$count = $Comment->where("goods_code=$goodsId")->count();
        $Page = new Page ($count , 3 );
        $show = $Page->showGoods();
        $commentInfo = $Comment->limit( $Page->firstRow . ',' . $Page->listRows )->where("goods_code=$goodsId")->select();

		$this->assign('goodsinfo',$goodsInfo);
		$this->assign('clstar',$clStar);
		$this->assign('commentInfo',$commentInfo);
		$this->assign('pager',$show);
		$this->display();
	}

	public function addComment(){
		$content = $_GET['content'];
		$goodsCode = $_GET['goodsCode'];
		$userUid = session(C('USER_AUTH_KEY'));
		$userName = session('user_nickname');
		if( empty($userUid) ){
			//未登录
			$this->ajaxReturn(false);
		}else{
			$Comment = M('comment_info');
			$result = $Comment->where("goods_code=$goodsCode and user_code=$userUid")->select();
			if( empty($result) ){
				if( empty($content) ){
					$this->success('内容不能为空');
				}
				$data['user_code'] = $userUid;
				$data['user_name'] = $userName;
				$data['goods_code'] = $goodsCode;
				$data['content'] = $content;
				$data['create_time'] = date('Y-m-d :H:i:s');
				
				if( $Comment->add($data) ){
					$html = '';
					$commtentInfo = $Comment->limit("0,2")->where("goods_code=$goodsCode")->order('create_time desc')->select();
					foreach($commtentInfo as $key=>$val){
						$html .= '<div class="i-m-box">
							<a href="" class="i-m-headimg" target="_blank"><img alt="" src="http://wwc.taobaocdn.com/avatar/getAvatar.do?userId=69382388&width=40&height=40&type=sns" /></a>
							<p class="i-m-user c9"><a class="nick" target="_blank" href="">'.$val['user_name'].'：</a>('.$val['create_time'].')<i class="bg"></i></p>
							<p class="i-m-txt-long c3">'.$val['content'].'</p>
						</div>';
					}
					$this->ajaxReturn($html);
				}
			}else{
				$this->ajaxReturn('end');
			}
		}
	}

	public function showComment(){
		import("ORG.Util.Page");
		$goodsId = $_GET['goodsCode'];
		$page = $_GET['p'];
		//查询评论
		$Comment = M('comment_info');
		$count = $Comment->where("goods_code=$goodsId")->count();
        $Page = new Page ($count , 3 ,$page);
        $show = $Page->showGoods();
        $commentInfo = $Comment->limit( $Page->firstRow . ',' . $Page->listRows )->where("goods_code=$goodsId")->select();
		foreach($commentInfo as $key=>$val){
			$html .= '<div class="i-m-box">
				<a href="" class="i-m-headimg" target="_blank"><img alt="" src="http://wwc.taobaocdn.com/avatar/getAvatar.do?userId=69382388&width=40&height=40&type=sns" /></a>
				<p class="i-m-user c9"><a class="nick" target="_blank" href="">'.$val['user_name'].'：</a>'.$val['create_time'].'<i class="bg"></i></p>
				<p class="i-m-txt-long c3">'.$val['content'].'</p>
			</div>';
		}
		$html .= '<div class="small-page">'.$show.'</div>';
		$this->ajaxReturn($html);
	}

	public function goodsOprate(){
		$goodsId = $_GET['id'];

        //验证是否登录

        $Goods = M('goods_base_info');
        $goodsInfo = $Goods->where("goods_code=$goodsId")->find();
        $data['goods_like_num'] = $goodsInfo['goods_like_num'] + 1;
        $Goods->where("goods_code=$goodsId")->save($data);

        $this->ajaxReturn($data['goods_like_num']);
	}
}
